using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.PlayerLoop;
using UnityEngine.UI;

public class SkillUI : MonoBehaviour
{
    public Image icon = null;
    public Image mask;
    public Skill_SO currentSkillData;
    private float CD;
    public Skill_List SkillList { get; set; }
    
    public int Index { get; set; } = -1;


    private void Update()
    {
        UpdateMask();
    }

    public void SetupSillUI(Skill_SO skill)
    {
        
        if (skill != null)
        {
            currentSkillData = skill;
            icon.sprite = skill.skillIcon;
            icon.gameObject.SetActive(true);
        }
        else
        {
            icon.gameObject.SetActive(false);
        }
    }
    
    public Skill_SO getSkillSo()
    {
        return SkillList.items[Index];
    }

    public void UpdateMask()
    {
        if (GameManager.Instance.playerStats.characterData.characterType == CharacterType.Saber)
        {
            if (!SkillManager.Instance.HaveCD[Index])
            {
                mask.fillAmount = 0;
            }
            else
            {
                mask.fillAmount = SkillManager.Instance.CDtemp[Index] / SkillManager.Instance.CD[Index];
            }
        }
        if (GameManager.Instance.playerStats.characterData.characterType == CharacterType.Archer)
        {
            if (!SkillManager.Instance.HaveCD[4+Index])
            {
                mask.fillAmount = 0;
            }
            else
            {
                mask.fillAmount = SkillManager.Instance.CDtemp[4+Index] / SkillManager.Instance.CD[4+Index];
            }
        }
        if (GameManager.Instance.playerStats.characterData.characterType == CharacterType.Mage)
        {
            if (!SkillManager.Instance.HaveCD[8+Index])
            {
                mask.fillAmount = 0;
            }
            else
            {
                mask.fillAmount = SkillManager.Instance.CDtemp[8+Index] / SkillManager.Instance.CD[8+Index];
            }
        }
    }


    public void StartMask(int value)
    {
        if (mask.fillAmount > 0f)
        {
            CD = currentSkillData.coolDown;
            mask.gameObject.SetActive(true);
            mask.fillAmount -= 1 / CD * Time.deltaTime;
        }
        else
        {
            mask.gameObject.SetActive(false);
            mask.fillAmount = 1;
            SkillManager.Instance.HaveCD[value] = false;
        }
    }
    
}
